**************************************************
* MAIN TABLES
**************************************************

******************************
* START
******************************

cap log close
set more off

clear
clear matrix
clear mata

set maxvar 9999
set mem 999999

log using Main_Tables.log, replace




************************************************************************************************************************************************
******************************************************* TABLE 1 - MIGRATION ********************************************************************
************************************************************************************************************************************************

use "bv_mm_mig_main_data.dta", clear

* generate village shocks
global eshocks="spei18"

* generate household shocks
global shocks="sany123"



global mig="bmig_w nmig_w"


foreach i in $mig {

	gen aux_`i'=`i' if round==1
	by hh_id: egen `i'_bl=max(aux_`i')
	drop aux_`i'
	
}

foreach i in $mig {

	gen aux_`i'=`i' if round==1
	by hh_id: egen b_`i'=max(aux_`i')
	drop aux_`i'
	
	* year 2013
	regress `i' treat i.treatblock b_`i' if (time13==1), cluster(loc_id)
	estimates store `i'_13_fanc, title("`i' 13 f anc")
	sum `i' if e(sample) & treat==0
	scalar define m_`i'_13_f_anc=r(mean)
	display m_`i'_13_f_anc	
	di e(r2_a)
		
	* year 2014	
	regress `i' treat i.treatblock b_`i' if (time14==1), cluster(loc_id)
	estimates store `i'_14_fanc, title("`i' 14 f anc")
	sum `i' if e(sample) & treat==0
	scalar define m_`i'_14_f_anc=r(mean)
	display m_`i'_14_f_anc
	di e(r2_a)
	

	drop b_`i'
	

estimates clear

}



*** village shocks ***

foreach i in $mig {

	gen aux_`i'=`i' if round==1
	by hh_id: egen b_`i'=max(aux_`i')
	drop aux_`i'

foreach j in $eshocks {

	gen treat_`j'=treat*`j'
	gen treatpost_`j'=treatpost*`j'
	gen post_`j'=`j' if (round==2 | round==3)
	replace post_`j'=0 if round==1
	
	
	regress `i' treat_`j' treat `j' i.treatblock  b_`i' if time13==1, cluster(loc_id)
	estimates store `i'_`j'_13_fanc, title("`i' `j' 13 f anc")
	sum `i' if e(sample) & treat==0
	scalar define m_`i'_`j'_13_f_anc=r(mean)
	display m_`i'_`j'_13_f_anc
	di e(r2_a)

	
	lincom treat + treat_`j'
	local F5c = r(estimate)
	local F5se = r(se)
	
	lincom treat + `j' + treat_`j'
	local F7c = r(estimate)
	local F7se = r(se)
	
	drop treat_`j' treatpost_`j' post_`j'
	


estimates clear
}
drop b_`i'
}


*** household shocks ***

foreach i in $mig {

	gen aux_`i'=`i' if round==1
	by hh_id: egen b_`i'=max(aux_`i')
	drop aux_`i'

foreach j in $shocks {

	gen treat_`j'=treat*`j'
	gen treatpost_`j'=treatpost*`j'
	gen post_`j'=`j' if (round==2 | round==3)
	replace post_`j'=0 if round==1
	

	
	regress `i' treat_`j' treat `j' i.treatblock b_`i' if time14==1, cluster(loc_id)
	estimates store `i'_`j'_14_fanc, title("`i' `j' 14 f anc")
	sum `i' if e(sample) & treat==0
	scalar define m_`i'_`j'_14_f_anc=r(mean)
	display m_`i'_`j'_14_f_anc
	di e(r2_a)
	

	
	lincom treat + treat_`j'
	local F5c = r(estimate)
	local F5se = r(se)
	
	lincom treat + `j' + treat_`j'
	local F7c = r(estimate)
	local F7se = r(se)
		
	drop treat_`j' treatpost_`j' post_`j'


estimates clear

}
drop b_`i'
}




************************************************************************************************************************************************
****************************************************** TABLE 2 - REMITTANCES *******************************************************************
************************************************************************************************************************************************

use "bv_mm_mig_data.dta", clear

* generate village shocks
global eshocks="spei18"

* generate household shocks
global shocks="sany123"


global rem="rrb lrtz"



foreach i in $rem {

	gen aux_`i'=`i' if round==1
	by hh_id: egen `i'_bl=max(aux_`i')
	drop aux_`i'
	
}



foreach i in $rem {

	gen aux_`i'=`i' if round==1
	by hh_id: egen b_`i'=max(aux_`i')
	drop aux_`i'
	
	
	
	regress `i' treat i.treatblock b_`i' if (time13==1), cluster(loc_id)
	estimates store `i'_13_fanc, title("`i' 13 f anc")
	sum `i' if e(sample) & treat==0
	scalar define m_`i'_13_f_anc=r(mean)
	display m_`i'_13_f_anc	
	di e(r2_a)
		

	
	regress `i' treat i.treatblock b_`i' if (time14==1), cluster(loc_id)
	estimates store `i'_14_fanc, title("`i' 14 f anc")
	sum `i' if e(sample) & treat==0
	scalar define m_`i'_14_f_anc=r(mean)
	display m_`i'_14_f_anc
	di e(r2_a)

	drop b_`i'


estimates clear

}


*** village shocks ***

foreach i in $rem {

	gen aux_`i'=`i' if round==1
	by hh_id: egen b_`i'=max(aux_`i')
	drop aux_`i'

foreach j in $eshocks {

	gen treat_`j'=treat*`j'
	gen treatpost_`j'=treatpost*`j'
	gen post_`j'=`j' if (round==2 | round==3)
	replace post_`j'=0 if round==1
	

	
	regress `i' treat_`j' treat `j' i.treatblock  b_`i' if time13==1, cluster(loc_id)
	estimates store `i'_`j'_13_fanc, title("`i' `j' 13 f anc")
	sum `i' if e(sample) & treat==0
	scalar define m_`i'_`j'_13_f_anc=r(mean)
	display m_`i'_`j'_13_f_anc
	di e(r2_a)
			
	
	lincom treat + treat_`j'
	local F5c = r(estimate)
	local F5se = r(se)
	
	lincom treat + `j' + treat_`j'
	local F7c = r(estimate)
	local F7se = r(se)
		
	drop treat_`j' treatpost_`j' post_`j'


estimates clear

}
drop b_`i'
}




*** household shocks ***

foreach i in $rem {

	gen aux_`i'=`i' if round==1
	by hh_id: egen b_`i'=max(aux_`i')
	drop aux_`i'

foreach j in $shocks {

	gen treat_`j'=treat*`j'
	gen treatpost_`j'=treatpost*`j'
	gen post_`j'=`j' if (round==2 | round==3)
	replace post_`j'=0 if round==1
	
	
	regress `i' treat_`j' treat `j' i.treatblock b_`i' if time14==1, cluster(loc_id)
	estimates store `i'_`j'_14_fanc, title("`i' `j' 14 f anc")
	sum `i' if e(sample) & treat==0
	scalar define m_`i'_`j'_14_f_anc=r(mean)
	display m_`i'_`j'_14_f_anc
	di e(r2_a)

	
	lincom treat + treat_`j'
	local F5c = r(estimate)
	local F5se = r(se)
	
	lincom treat + `j' + treat_`j'
	local F7c = r(estimate)
	local F7se = r(se)

	drop treat_`j' treatpost_`j' post_`j'


estimates clear

}
drop b_`i'
}



************************************************************************************************************************************************
****************************************************** TABLE 3 - AGRICULTURE *******************************************************************
************************************************************************************************************************************************

use "bv_mm_mig_data.dta", clear

* generate village shocks
global eshocks="spei18"

* generate household shocks
global shocks="sany123"


global ag="activef ag_inv"


foreach i in $ag {

	gen aux_`i'=`i' if round==1
	by hh_id: egen `i'_bl=max(aux_`i')
	drop aux_`i'
	
}


foreach i in $ag {

	gen aux_`i'=`i' if round==1
	by hh_id: egen b_`i'=max(aux_`i')
	drop aux_`i'

	
	regress `i' treat i.treatblock b_`i' if (time13==1), cluster(loc_id)
	estimates store `i'_13_fanc, title("`i' 13 f anc")
	sum `i' if e(sample) & treat==0
	scalar define m_`i'_13_f_anc=r(mean)
	display m_`i'_13_f_anc	
	di e(r2_a)
	
	
	
	regress `i' treat i.treatblock b_`i' if (time14==1), cluster(loc_id)
	estimates store `i'_14_fanc, title("`i' 14 f anc")
	sum `i' if e(sample) & treat==0
	scalar define m_`i'_14_f_anc=r(mean)
	display m_`i'_14_f_anc
	di e(r2_a)
	

	
	drop b_`i'


estimates clear

}


*** village shocks ***

foreach i in $ag {

	gen aux_`i'=`i' if round==1
	by hh_id: egen b_`i'=max(aux_`i')
	drop aux_`i'

foreach j in $eshocks {

	gen treat_`j'=treat*`j'
	gen treatpost_`j'=treatpost*`j'
	gen post_`j'=`j' if (round==2 | round==3)
	replace post_`j'=0 if round==1
	
	
	regress `i' treat_`j' treat `j' i.treatblock  b_`i' if time13==1, cluster(loc_id)
	estimates store `i'_`j'_13_fanc, title("`i' `j' 13 f anc")
	sum `i' if e(sample) & treat==0
	scalar define m_`i'_`j'_13_f_anc=r(mean)
	display m_`i'_`j'_13_f_anc
	di e(r2_a)

	
	lincom treat + treat_`j'
	local F5c = r(estimate)
	local F5se = r(se)
	
	lincom treat + `j' + treat_`j'
	local F7c = r(estimate)
	local F7se = r(se)
	
	drop treat_`j' treatpost_`j' post_`j'


estimates clear

}
drop b_`i'
}



*** household shocks ***

foreach i in $ag {

	gen aux_`i'=`i' if round==1
	by hh_id: egen b_`i'=max(aux_`i')
	drop aux_`i'

foreach j in $shocks {

	gen treat_`j'=treat*`j'
	gen treatpost_`j'=treatpost*`j'
	gen post_`j'=`j' if (round==2 | round==3)
	replace post_`j'=0 if round==1
	
	
	regress `i' treat_`j' treat `j' i.treatblock b_`i' if time14==1, cluster(loc_id)
	estimates store `i'_`j'_14_fanc, title("`i' `j' 14 f anc")
	sum `i' if e(sample) & treat==0
	scalar define m_`i'_`j'_14_f_anc=r(mean)
	display m_`i'_`j'_14_f_anc
	di e(r2_a)
	
	lincom treat + treat_`j'
	local F5c = r(estimate)
	local F5se = r(se)
	
	lincom treat + `j' + treat_`j'
	local F7c = r(estimate)
	local F7se = r(se)
		
	drop treat_`j' treatpost_`j' post_`j'

estimates clear

}
drop b_`i'
}



**************************************************************************************************************************
************************************** TABLE 4 - WELL-BEING, EXPENDITURE, VULNERABIITY *********************************** **************************************************************************************************************************

use "bv_mm_mig_data.dta", clear

* generate village shocks
global eshocks="spei18"

* generate household shocks
global shocks="sany123"


global well="subj lycqp vul2"


foreach i in $well {

	gen aux_`i'=`i' if round==1
	by hh_id: egen `i'_bl=max(aux_`i')
	drop aux_`i'
	
}


foreach i in $well {

	gen aux_`i'=`i' if round==1
	by hh_id: egen b_`i'=max(aux_`i')
	drop aux_`i'
		
	
	regress `i' treat i.treatblock b_`i' if (time13==1), cluster(loc_id)
	estimates store `i'_13_fanc, title("`i' 13 f anc")
	sum `i' if e(sample) & treat==0
	scalar define m_`i'_13_f_anc=r(mean)
	display m_`i'_13_f_anc	
	di e(r2_a)
	
	regress `i' treat i.treatblock b_`i' if (time14==1), cluster(loc_id)
	estimates store `i'_14_fanc, title("`i' 14 f anc")
	sum `i' if e(sample) & treat==0
	scalar define m_`i'_14_f_anc=r(mean)
	display m_`i'_14_f_anc	
	di e(r2_a)
	

	
	drop b_`i'


estimates clear

}



*** village shocks ***

foreach i in $well {

	gen aux_`i'=`i' if round==1
	by hh_id: egen b_`i'=max(aux_`i')
	drop aux_`i'

foreach j in $eshocks {

	gen treat_`j'=treat*`j'
	gen treatpost_`j'=treatpost*`j'
	gen post_`j'=`j' if (round==2 | round==3)
	replace post_`j'=0 if round==1
 
	 
	regress `i' treat_`j' treat `j' i.treatblock b_`i' if time13==1, cluster(loc_id)
	estimates store `i'_`j'_13_fanc, title("`i' `j' 13 f anc")
	sum `i' if e(sample) & treat==0
	scalar define m_`i'_`j'_13_f_anc=r(mean)
	display m_`i'_`j'_13_f_anc
	di e(r2_a)
		
	
	lincom treat + treat_`j'
	local F5c = r(estimate)
	local F5se = r(se)
	
	lincom treat + `j' + treat_`j'
	local F7c = r(estimate)
	local F7se = r(se)
	
	drop treat_`j' treatpost_`j' post_`j'


estimates clear

}
drop b_`i'
}



*** household shocks ***

foreach i in $well {

	gen aux_`i'=`i' if round==1
	by hh_id: egen b_`i'=max(aux_`i')
	drop aux_`i'

foreach j in $shocks {

	gen treat_`j'=treat*`j'
	gen treatpost_`j'=treatpost*`j'
	gen post_`j'=`j' if (round==2 | round==3)
	replace post_`j'=0 if round==1
	
	
	regress `i' treat_`j' treat `j' i.treatblock b_`i' if time14==1, cluster(loc_id)
	estimates store `i'_`j'_14_fanc, title("`i' `j' 14 f anc")
	sum `i' if e(sample) & treat==0
	scalar define m_`i'_`j'_14_f_anc=r(mean)
	display m_`i'_`j'_14_f_anc
	di e(r2_a)
		
	
	lincom treat + treat_`j'
	local F5c = r(estimate)
	local F5se = r(se)
	
	lincom treat + `j' + treat_`j'
	local F7c = r(estimate)
	local F7se = r(se)
	
	drop treat_`j' treatpost_`j' post_`j'


estimates clear
}
drop b_`i'
}




************************************************************
*** Std error correction for multiple hypotheses testing ***
************************************************************

gen treat_spei18=treat*spei18
gen treatpost_spei18=treatpost*spei18


gen treat_sany123=treat*sany123
gen treatpost_sany123=treatpost*sany123



*** Table 1

global mig="bmig_w nmig_w"


foreach i in $mig {

	gen aux_`i'=`i' if round==1
	by hh_id: egen `i'_bl=max(aux_`i')
	drop aux_`i'
	
}

rwolf bmig_w nmig_w if (time13==1), indepvar(treat)  method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl) verbose cluster(loc_id)

rwolf bmig_w nmig_w if (time14==1), indepvar(treat)  method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl)  verbose cluster(loc_id)


* generate interaction between treatment and shocks and time


rwolf bmig_w nmig_w if (time13==1), indepvar(treat_spei18)   controls(treat spei18) method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl) verbose cluster(loc_id)


rwolf bmig_w nmig_w if (time13==1), indepvar(treat)   controls(treat_spei18 spei18) method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl) verbose cluster(loc_id)


rwolf bmig_w nmig_w if (time13==1), indepvar(spei18)   controls(treat treat_spei18) method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl) verbose cluster(loc_id)


* generate interaction between treatment and shocks and time



rwolf nmig_w bmig_w if (time14==1), indepvar(treat_sany123)   controls(treat sany123) method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl) verbose cluster(loc_id)


rwolf nmig_w bmig_w if (time14==1), indepvar(treat)   controls(treat_sany123 sany123) method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl) verbose cluster(loc_id)


rwolf nmig_w bmig_w if (time14==1), indepvar(sany123)   controls(treat treat_sany123) method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl) verbose cluster(loc_id)



*** Table 2


global rem="rrb lrtz"



foreach i in $rem {

	gen aux_`i'=`i' if round==1
	by hh_id: egen `i'_bl=max(aux_`i')
	drop aux_`i'
	
}


rwolf rrb lrtz if (time13==1), indepvar(treat)  method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl) verbose cluster(loc_id)

rwolf rrb lrtz if (time14==1), indepvar(treat)  method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl)  verbose cluster(loc_id)



* generate interaction between treatment and shocks and time

rwolf rrb lrtz if (time13==1), indepvar(treat_spei18)   controls(treat spei18) method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl) verbose cluster(loc_id)

rwolf rrb lrtz if (time13==1), indepvar(treat)   controls(treat_spei18 spei18) method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl) verbose cluster(loc_id)

rwolf rrb lrtz if (time13==1), indepvar(spei18)   controls(treat treat_spei18) method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl) verbose cluster(loc_id)


* generate interaction between treatment and shocks and time


rwolf rrb lrtz if (time14==1), indepvar(treat_sany123)   controls(treat sany123) method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl) verbose cluster(loc_id)


rwolf rrb lrtz if (time14==1), indepvar(treat)   controls(treat_sany123 sany123) method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl) verbose cluster(loc_id)


rwolf rrb lrtz if (time14==1), indepvar(sany123)   controls(treat treat_sany123) method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl) verbose cluster(loc_id)



*** Table 3


global ag="activef ag_inv"


foreach i in $ag {

	gen aux_`i'=`i' if round==1
	by hh_id: egen `i'_bl=max(aux_`i')
	drop aux_`i'
	
}



rwolf activef ag_inv if (time13==1), indepvar(treat)  method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl) verbose cluster(loc_id)

rwolf activef ag_inv if (time14==1), indepvar(treat)  method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl)  verbose cluster(loc_id)

	

* generate interaction between treatment and shocks and time


rwolf activef ag_inv if (time13==1), indepvar(treat_spei18)   controls(treat spei18) method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl) verbose cluster(loc_id)

rwolf activef ag_inv if (time13==1), indepvar(treat)   controls(treat_spei18 spei18) method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl) verbose cluster(loc_id)

rwolf activef ag_inv if (time13==1), indepvar(spei18)   controls(treat treat_spei18) method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl) verbose cluster(loc_id)



* generate interaction between treatment and shocks and time


rwolf activef ag_inv if (time14==1), indepvar(treat_sany123)   controls(treat sany123) method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl) verbose cluster(loc_id)

rwolf activef ag_inv if (time14==1), indepvar(treat)   controls(treat_sany123 sany123) method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl) verbose cluster(loc_id)

rwolf activef ag_inv if (time14==1), indepvar(sany123)   controls(treat treat_sany123) method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl) verbose cluster(loc_id)


	

*** Table 4


rwolf subj lycqp vul2 if (time13==1), indepvar(treat)  method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl) verbose cluster(loc_id)

rwolf subj lycqp vul2 if (time14==1), indepvar(treat)  method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl)  verbose cluster(loc_id)


* generate interaction between treatment and shocks and time


rwolf subj lycqp vul2 if (time13==1), indepvar(treat_spei18) controls(treat spei18) method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl) verbose cluster(loc_id)

rwolf subj lycqp vul2 if (time13==1), indepvar(treat) controls(treat_spei18 spei18) method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl) verbose cluster(loc_id)

rwolf subj lycqp vul2 if (time13==1), indepvar(spei18) controls(treat treat_spei18) method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl) verbose cluster(loc_id)


* generate interaction between treatment and shocks and time

rwolf subj lycqp vul2 if (time14==1), indepvar(treat_sany123)   controls(treat sany123) method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl) verbose cluster(loc_id)

rwolf subj lycqp vul2 if (time14==1), indepvar(treat)   controls(treat_sany123 sany123) method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl) verbose cluster(loc_id)

rwolf subj lycqp vul2 if (time14==1), indepvar(sany123)   controls(treat treat_sany123) method(areg) abs(treatblock) reps(1000) seed(51) vce(cluster loc_id) bl(_bl) verbose cluster(loc_id)






log close
